import { expect } from '@jest/globals';
import * as fse from 'fs-extra';
import path from 'path';

import { runHvigorAsync } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single, 
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
};

/**
 *  entry依赖hsp，entry的build-profile.json5中添加targets，同步编译构建报错。
 *
 * @param {Project} project
 * @param {TestTaskOption} taskConfig
 * @param {ICaseRecord} record
 * @return res
 */
const testfun: TestCaseFun = async (project, taskConfig, record) => {
  // 1. 创建API10 stage 工程 ，添加sharelibrary模块，在entry的oh-package.json5文件中配置hsp依赖
  const entryPath = project.getModule('entry').moduleRootPath();
  const fileStr = '' +
        '@Component\n' +
        'export struct test {\n' +
        '  @State message: string = \'123\'\n' +
        '\n' +
        '  build() {\n' +
        '    Row() {\n' +
        '      Column() {\n' +
        '        Text(this.message)\n' +
        '          .fontSize(50)\n' +
        '          .fontWeight(FontWeight.Bold)\n' +
        '      }\n' +
        '      .width(\'100%\')\n' +
        '    }\n' +
        '    .height(\'100%\')\n' +
        '  }\n' +
        '}';

  await fse.writeFile(path.resolve(entryPath, 'src/main/ets/pages/test.ets'), fileStr, 'utf-8');

  const fileBuff = await fse.readFile(path.resolve(entryPath, 'src/main/ets/pages/Index.ets'));
  const fileData = fileBuff.toString().split('\r\n');

  let fileWrite = 'import {test} from "../pages/test"\r\n';
  fileData.forEach((item,index) => {
    if (item.indexOf('Text') !== -1){
      fileWrite += '        test()\r\n';
    }
    fileWrite += item;
    if (index !== fileData.length - 1) {
      fileWrite += '\r\n';
    }
  });

  await fse.writeFile(path.resolve(entryPath, 'src/main/ets/pages/Index.ets'), fileWrite, 'utf-8');

  // 3. 执行同步编译构建
  const commands: string[] =
        '--mode module -p product=default assembleHap --analyze=normal --parallel --incremental --daemon'.split(' ');
  const result = await runHvigorAsync(project.projectRootPath(), commands);
  record.addStep('执行同步编译构建', result);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
};

TestCaseBuilder.builder()
  .caseName('OPEN_LAB_AIDE_ZOS_module_buildHap_HOS_API10_STAGE_023')
  .description('支持通过import  from ets方式构建')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L1).build())
  .testFun(testfun)
  .load();
